import ListNode from "../common/list_node"
import { a1, b1, c1 } from '../common/list_node'

//思路，用数组装之前的元素，直到中间值出现。然后一个一个跟数组元素比较
function isPalindrome_self(head: ListNode | null): boolean {
  if (head == null) return false

  let left: ListNode[] = []//数组
  let leftIndex = 0//数组中遍历的当前索引


  let preNode:ListNode | null = null
  let currentNode:ListNode | null = head

  let find_border = false
  let isP = false
  while (currentNode != null) {
    let preVal = getVal(preNode)
    let currentVal = getVal(preNode)
    let nextVal = getVal(currentNode.next)
    //三种情况，还未到临界，刚好临界，过了临界
    //临界条件:pre.val == current.val 或者pre.val == current.next.val 

    if(find_border == true){//过了临界
      if(leftIndex == 0 && currentNode.next == null) {
        isP = true
      }
      if(leftIndex > 0) {break}
      //比较索引跟当前
      let ele = left[leftIndex]
      if(getVal(ele) == getVal(currentNode)) {
        continue
      }else {
        break
      }

    }else if(preVal == currentVal || preVal == nextVal) {//刚好临界

      find_border = true//到达临界条件
      leftIndex = left.length - 1//设置初始索引
      if(preVal == currentVal) {
        //currentVal 不动
      }
      if(preVal == nextVal) {
        currentNode = currentNode.next
      }

    }else {//还没有到达临界条件,刚开始会一直执行这个case
      left.push(currentNode!)
      currentNode = currentNode.next
    }
    
  }
  return isP
};

function isPalindrome(head: ListNode | null): boolean {
  if (head == null) {
    return false
  }
  let nodes:ListNode[] = []
  while(head != null) {
    nodes.push(head)
    head = head.next
  }
  for (let i = 0,j = nodes.length - 1; i < j; ++i,--j) {
    const left = nodes[i];
    const right = nodes[j];
    if(left.val != right.val) {
      return false
    }
    
  }
  return true
  
}
//遍历第一遍，分成两半-》后半部分反转，再比较
function isPalindrome_reverse(head: ListNode | null): boolean {return false}

function getVal(head: ListNode | null): number|null {
  if(head == null) return null
  return head.val
}
let ret = isPalindrome(c1)
console.log(ret)